package com.badger.spring.boot.security.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.Arrays;
import java.util.List;

/**
 * 基于Swagger生成API文档
 * @author: liqi
 */
@Configuration
@EnableSwagger2
public class SwaggerConfiguration {

    static final String TEST_TOKEN_101 = "";

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select()
                .apis(RequestHandlerSelectors.basePackage("com.badger.spring.boot.security")).paths(PathSelectors.any())
                .build().globalOperationParameters(parameters());
    }

    private List<Parameter> parameters() {
        ParameterBuilder ticketPar = new ParameterBuilder();
        ticketPar.name("Authorization");
        ticketPar.description("验证的token,测试使用");
        ticketPar.modelRef(new ModelRef("string"));
        ticketPar.defaultValue(TEST_TOKEN_101);
        ticketPar.parameterType("header");
        ticketPar.required(false);
        return Arrays.asList(ticketPar.build());
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder().title("安全框架").description("内部接口").version("1.0.0-RELEASE").build();
    }
}